package palette;

import javax.swing.*;
import java.awt.*;
//import java.awt.geom.Line2D;

import cuShapes.*;
import cuShapes.Box;


/**
* Contains the Canvas put into the Canvas Windows and it controls the drawing of shapes
* whenever the shapeList updates itself.
*/
public class Canvas extends JPanel implements ShapeListObserver{

	final static BasicStroke thinStroke = new BasicStroke(2.0f);
	final static BasicStroke wideStroke = new BasicStroke(8.0f);
	private static final long serialVersionUID = 1L;
	private Paint bgColor = Color.white;
	private Paint brushColor = Color.black;
	private Boolean hasGrid = false;
	private ShapeList shapeList;
	
	/**********************************************
	 *   CONSTRUCTOR
	**********************************************/
	public Canvas(){
		this.setPreferredSize(new Dimension(500,400));
		this.setVisible(true);		
	}

	/**********************************************
	 *   paint() Draws the lines onto the screen
	 *   
	 *   Normally would be called as repaint, such as in the update
	 *   method.
	**********************************************/
	public void paint(Graphics g){
		
		Graphics2D g2 = (Graphics2D)g;
		
		//fill background
	    g2.setPaint(bgColor);
	    g2.fillRect(0, 0, this.getWidth(), this.getHeight());

	    //draw grid
	    if (hasGrid) 
	    {
	    	drawGrid(g);
	    }
	    
	    //draw all shapes
	    g2.setPaint(brushColor);
	    if (shapeList != null) {
	    	drawShapes(g);
	    }
	}
	
	/**
	 * drawShapes is called by paint and is used to draw all the shapes in the shapeList
	 * @param g
	 */
	
	public void drawShapes(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		g2.setStroke(thinStroke);
		g2.setColor(Color.black);

		if(shapeList.getCurrentShape() != null){
			shapeList.getCurrentShape().drawToCanvas(g2);
			//sidebar.setShape(currentShape);
		}
		
		//draw all the rest
		for(PQShape x : shapeList){
			x.drawToCanvas(g2);
			
			//Draw Connections
			Point xCenter = new Point((x.getStart().x + x.getEnd().x)/2, 
					(x.getStart().y + x.getEnd().y)/2);
			for(Connectable c : ((Connectable)x).getConnections()) {
				Point cCenter = new Point((c.getStart().x + c.getEnd().x)/2, 
						(c.getStart().y + c.getEnd().y)/2);				
				(new Arrow(xCenter, cCenter, Color.black)).drawToCanvas(g2);
			}
		}
	}
	
	public void setCurrentShape(PQShape p) {
		shapeList.setCurrentShape(p);
	}
	
	/******************************************
	 *  Clears the contents of the shape list
	 *  
	 *  changed from local array list .clear(), to
	 *  shapeDrawer.clear() (a new method that clears the
	 *  arrayList in the shapeDrawer.
	 *    -JD  3.30.10
	 */
	public void clear() {
		shapeList.clear();
		Box.resetNames();
		Ellipse.resetNames();
		repaint();
	}
	
	/**
	 * Called by paint when the grid button is clicked to draw a grid
	 * @param g
	 */
	
	public void drawGrid(Graphics g) {
		
		int wide = getWidth();
		int tall = getHeight();

		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(Color.lightGray);

		g2.setStroke(new BasicStroke(1));
		// the vertical lines
		for (int i = 10; i < wide; i+=10) {
			g2.drawLine(i, 0, i, tall);
		}
		// the horizontal lines
		for (int i = 10; i < tall; i+=10) {
			g2.drawLine(0, i, wide, i);
		}
	}
	
	public void setGrid(Boolean b) {
		hasGrid = b;
	}

	@Override
	public void update(ShapeList s) {
		shapeList = s;
		repaint();
	}
}
